<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="/backstage/css/font.css">
    <link rel="stylesheet" href="/backstage/css/xadmin.css">
    <link rel="stylesheet" href="/common/webuploader/webuploader.css">
    <style>
        /*.layui-upload-drag div{*/
           /**/
        /*}*/

        .webuploader-pick{
            width:100% !important;
            /*height:80%;*/
            left:0 !important;
            padding:30px 0 !important;
            background: #eee !important;
            color:#000;
        }

    </style>
</head>
<body style="width:100%;height:100%;">


<!--<div class="layui-upload">-->
<!--<button type="button" class="layui-btn" id="test2">多图片上传</button> -->
<!--<blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">-->
<!--预览图：-->
<!--<div class="layui-upload-list" id="demo2"></div>-->
<!--</blockquote>-->
<!--</div>-->
<div style="width:90%;height:70%;" class="layui-upload-drag" id="test10" ><i class="layui-icon"></i>
    <p>点击上传，或将文件拖拽到此处</p>
    <div id="uploadPatchForm" style="display: none" class="roundRect">
            
        <div id="uploadLoadingDiv">
            <div class="layui-progress" lay-showpercent="true" lay-filter="demo">
                     
                <!--<div class="layui-progress-bar layui-bg-green progress" lay-percent="0%"></div>-->
                <div class="layui-progress-bar layui-bg-green progress" lay-percent="">
                </div>
                <div class="ratio"></div>
            </div>
                
        </div>
    </div>
</div>

</body>
<script type="text/javascript" src="/js/jquery.js"></script>
<script type="text/javascript" src="/backstage/lib/layui/layui.js"></script>
<script type="text/javascript" src="/common/webuploader/webuploader.js"></script>
<script type="text/javascript" src="/backstage/js/xadmin.js"></script>

<script>
    var type = '{$type}';
    var exts = '';
    var file_type = '';

    switch (type) {
        case '1':
            exts = 'jpg|jpeg|png|gif';
            file_type = 'image';
            break;
        case '2':
            exts = 'mp3';
            file_type = 'audio';

            break;
        case '3':
            exts = 'mp4';
            file_type = 'video';

            break;

    }
    // 初始化Web Uploader
    var uploader = WebUploader.create({

        // 选完文件后，是否自动上传。
        auto: true,

        // swf文件路径
        swf: '/commmon/webuploader/Uploader.swf',

        // 文件接收服务端。
//        server: '/backstage/resource/webUpload?file_type={$type}&folder_id={$folder_id}',
        server: '/backstage/resource/webUpload?time={$time}&file_type={$type}',

        // 选择文件的按钮。可选。
        // 内部根据当前运行是创建，可能是input元素，也可能是flash.
        pick: '#test10',

//        disableGlobalDnd:true,
        dnd:'#test10',

        //是否分片上传
        chunked:true,

        //分片大小
        chunkSize:5120000,

        //上传并发数
        threads:5,
        // 只允许选择图片文件。
        accept: {
            title: '文件',
            extensions: exts,
            mimeTypes: file_type+'/*'
        }
    });


    // 文件上传过程中创建进度条实时显示。
    uploader.on('uploadProgress', function (file, percentage) {

        $('#uploadPatchForm').show();
        $('.progress').attr('lay-percent','50%')

        if(percentage == 1){
            percentage = 0.99
        }

        var num =  parseInt(percentage * 100);

        $('.progress').css('width', num + '%');
        $('.ratio').text(num + '%')

    });

    // 文件上传成功，给item添加成功class, 用样式标记上传成功。
    uploader.on('uploadSuccess', function (file,res) {
//        $('#' + file.id).addClass('upload-state-done');
//        console.log(file[0].ext,file['file'].name)
//        layer.closeAll();
        if (res.code == 1) {

            $.post('{:url("mergeFile",["file_type"=>$type,"folder_id"=>$folder_id])}',{dir:res.dir,ext:file.ext,file_name:file.name},function(res2){

//                console.log(res2);
                if (res2.code == 1) {
                    layer.msg("上传成功");

                    parent.app.getListData();
                    var index = parent.layer.getFrameIndex(window.name);
                    //关闭当前frame
                    parent.layer.close(index);
                    parent.layer.msg("上传成功", {icon: 1, time: 1200});
                } else {
                    layer.msg(res2.msg,{icon: 2, time: 1200},function(){
                        location.reload();
                    })

                }
            })

        } else {
//            layer.msg('上传失败')
            layer.msg('上传失败',{icon: 2, time: 1200},function(){
                location.reload();
            })

        }
    });

    // 文件上传失败，显示上传出错。
    uploader.on('uploadError', function (file) {

        layer.msg('上传失败',{icon: 2, time: 1200},function(){
            location.reload();
        })

    });

    //    // 完成上传完了，成功或者失败，先删除进度条。
    //    uploader.on('uploadComplete', function (file) {
    //        $('#' + file.id).find('.progress').remove();
    //    });

    // 当有文件添加进来的时候
    uploader.on('fileQueued', function (file) {

//        console.log(file);
        return;
        var $li = $(
                '<div id="' + file.id + '" class="file-item thumbnail">' +
                '<img>' +
                '<div class="info">' + file.name + '</div>' +
                '</div>'
            ),
            $img = $li.find('img');

        // $list为容器jQuery实例
        $list.append($li);

        // 创建缩略图
        // 如果为非图片文件，可以不用调用此方法。
        // thumbnailWidth x thumbnailHeight 为 100 x 100
        uploader.makeThumb(file, function (error, src) {
            if (error) {
                $img.replaceWith('<span>不能预览</span>');
                return;
            }

            $img.attr('src', src);
        }, thumbnailWidth, thumbnailHeight);
    });
</script>
</html>